package com.yc.navy.goods.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yc.navy.goods.dto.CartInfoDTO;
import com.yc.navy.goods.dto.ConstantInfoDTO;
import com.yc.navy.goods.dto.MemberInfoDTO;
//import com.yc.navy.goods.dto.MemberInfoDTO;
import com.yc.navy.goods.enums.ResultEnum;
import com.yc.navy.goods.service.ICartInfoService;
import com.yc.navy.goods.vo.ResultVO;

@RestController
@RequestMapping("/cart")
public class CartInfoController {
	@Autowired
	private ICartInfoService cartInfoService;
	
	@Autowired
	private RedisTemplate<String,Object> redisTemplate;
	
	private String getLogin(HttpSession session) {
		Object obj = session.getAttribute(ConstantInfoDTO.LOGINCHECK);
		if(obj == null) {
			return null;
		}
		MemberInfoDTO m = (MemberInfoDTO) obj;
		return "m_" + m.getMid();
	}
	
	@RequestMapping("/add")
	public ResultVO add(HttpSession session,String bid,Integer num) {
		String mid = getLogin(session);
		if(mid == null || "".equals(mid)) {
			return new ResultVO(ResultEnum.NOTLOGIN);
		}
		
		try {
			bid = "b_" + bid;
			if(redisTemplate.opsForHash().hasKey(mid, bid)) {
				redisTemplate.opsForHash().increment(mid, bid, num);
			}else {
				redisTemplate.opsForHash().put(mid, bid, num);
			}
			return new ResultVO(ResultEnum.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ResultVO(ResultEnum.ERROR);
	}
	
	@RequestMapping("/findList")
	public ResultVO findList(HttpSession session) {
		String mid = getLogin(session);
		if(mid == null || "".equals(mid)) {
			return new ResultVO(ResultEnum.NOTLOGIN);
		}
		
		try {
			Map<Object,Object> map = redisTemplate.opsForHash().entries(mid);
			if(map == null || map.isEmpty()) {
				return new ResultVO(ResultEnum.NOT_DATA);
			}
			
			List<String> pnos = map.keySet().stream().map(item ->String.valueOf(item).replace("b_", "")).collect(Collectors.toList());
			List<CartInfoDTO> list = cartInfoService.findByPnos(pnos);
			list.forEach(item ->{
				item.setNum(Integer.parseInt(String.valueOf(map.get("b_" + item.getBid()))));
			});
			return new ResultVO(ResultEnum.SUCCESS, list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return new ResultVO(ResultEnum.ERROR);
	}
	
	@RequestMapping("/del")
	public ResultVO delete(HttpSession session,@RequestParam("bid") Integer bid) {
		String mid = getLogin(session);
		if(mid == null || "".equals(mid)) {
			return new ResultVO(ResultEnum.NOTLOGIN);
		}
		try {
			redisTemplate.opsForHash().delete(mid,"b_"+bid);
			return new ResultVO(ResultEnum.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ResultVO(ResultEnum.ERROR);
	}
	
	@RequestMapping("/findByPnos")
	public ResultVO findByPnos(HttpSession session,String pnos) {
		String mid = getLogin(session);
		if(mid == null || "".equals(mid)) {
			return new ResultVO(ResultEnum.NOTLOGIN);
		}
		Map<Object, Object> map = redisTemplate.opsForHash().entries(mid);
		if(map == null || map.isEmpty()) {
			return new ResultVO(ResultEnum.NOT_DATA);
		}
		String [] str = pnos.split(",");
		List<CartInfoDTO> list = cartInfoService.findByPnos(Arrays.asList(str));
		if(list.isEmpty() || list.size() <= 0) {
			return new ResultVO(ResultEnum.NOTFOUND);
		}
		list.forEach(item ->{
			item.setNum(Integer.parseInt(String.valueOf(map.get("b_"+item.getBid()))));
		});
		return new ResultVO(ResultEnum.SUCCESS,list);
	}
}
